<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地块名" prop="landName">
        <el-input
          v-model="queryParams.landName"
          placeholder="请输入地块名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="作物代号" prop="cropsId">
        <el-input
          v-model="queryParams.cropsId"
          placeholder="请输入农作物代号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="0 休耕期,1 农闲期,2 农忙期" clearable>
          <el-option
            v-for="dict in dict.type.argo_land_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农业类型" prop="argoType">
        <el-select v-model="queryParams.argoType" placeholder="请选择农业类型" clearable>
          <el-option
            v-for="dict in dict.type.argo_land_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['land:info:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['land:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="地块代号" align="center" prop="id" />-->
      <el-table-column label="地块名" align="center" prop="landName" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="基地名" align="center" prop="deptName" />
      <el-table-column label="农作物" align="center" prop="cropsName" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.argo_land_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="位置" align="center" prop="address" />
      <el-table-column label="农业类型" align="center" prop="argoType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.argo_land_type" :value="scope.row.argoType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['land:info:edit']"
          >修改</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改地块信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="地块名" prop="landName">
          <el-input v-model="form.landName" placeholder="请输入地块名" />
        </el-form-item>
        <el-form-item label="基地" prop="deptName">
          <el-input readonly value=""  />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="农作物" prop="cropsId">
          <el-select v-model="form.cropsId" placeholder="请选择" clearable>
            <el-option
              v-for="item in cropsList"
              :key="item.id"
              :label="item.cropsName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="0 休耕期,1 农闲期,2 农忙期">
            <el-option
              v-for="dict in dict.type.argo_land_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="address">
          <el-input v-model="form.address" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="农业类型" prop="argoType">
          <el-select v-model="form.argoType" placeholder="请选择农业类型">
            <el-option
              v-for="dict in dict.type.argo_land_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/land/info";
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import request from '@/utils/request'

// 查询农作物(播种后)列表
export function listCrops(query) {
  return request({
    url: '/crops/info/list',
    method: 'get',
    params: query
  })
}
export default {
  name: "Info",
  dicts: ['argo_land_state', 'argo_land_type'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地块信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        landName: null,
        deptId: null,
        cropsId: null,
        state: null,
        address: null,
        argoType: null,
      },
      // 查询参数
      queryCropParams: {
        pageNum: 1,
        pageSize: 100,
        cropsName: null,
        cropsType: null,
        createTime: null,
        pickedTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //部门列表
      listDept: [],
      cropsList: []
    };
  },
  created() {
    this.getList();
    // 查询部门列表
    this.getDeptList();
    this.getCropsList();
  },
  methods: {
    /** 查询地块信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门列表 */
    getDeptList() {
      console.log("查询部门方法调用。。。")
      this.loading = true;
      listDept({
        pageNum: 1,
        pageSize: 100,
        deptName: null,
        status: 0
      }).then(response => {
        let i
        for ( i = 0; i < response.data.length; i++ ) {
          if ( response.data[i].ancestors.length == 9 ) {
            this.listDept.push(response.data[i]);
          }
        }
        console.log(this.listDept);
        this.loading = false;
      }).catch(error => {
        console.error(error);
      });
    },
    /** 查询农作物(播种后)列表 */
    getCropsList() {
      this.loading = true;
      this.queryCropParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryCropParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryCropParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangePickedTime && '' != this.daterangePickedTime) {
        this.queryCropParams.params["beginPickedTime"] = this.daterangePickedTime[0];
        this.queryCropParams.params["endPickedTime"] = this.daterangePickedTime[1];
      }
      listCrops(this.queryCropParams).then(response => {
        this.cropsList = response.rows;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        landName: null,
        image: null,
        deptId: null,
        cropsId: null,
        state: null,
        address: null,
        argoType: null,
        createTime: null,
        updateTime: null,
        createUser: null,
        updateUser: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加地块信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改地块信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除地块信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('land/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
